﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

using KorbeRTR.Models;

namespace KorbeRTR
{
    public partial class FormCurrentRecord : Form
    {
        public FormCurrentRecord()
        {
            InitializeComponent();
        }

        private void FormCurrentRecord_Load(object sender, EventArgs e)
        {
            InitGrid(grid1);

            //刷新数据
            var list = CurrentRecordView.Select(DTP_Start.Value.Date.Date, DTP_End.Value.Date.AddDays(1));
            UpdateGrid(grid1, list);
        }

        /// <summary>
        /// 初始化表格
        /// </summary>
        private void InitGrid(FlexCell.Grid grid)
        {
            //Grid1设置
            grid.AutoRedraw = false;
            grid.ExtendLastCol = false;

            // 设置行数/列数
            grid.Rows = 3;
            grid.Cols = 3 + 4 * Config.镀铜槽数;

            //这个包围区间要减1 居中
            grid.Range(0, 0, grid.Rows - 1, grid.Cols - 1).Alignment = FlexCell.AlignmentEnum.CenterCenter;
            grid.FrozenRows = 2;

            //隐藏首行首列
            grid.Row(0).Visible = false;
            grid.Column(0).Visible = false;

            // 设置行高/列宽
            for (int i = 0; i < grid.Cols; i++)
                grid.Column(i).Width = 60;
            grid.Column(2).Width = 120;

            // 设置单元格文字
            grid.Cell(1, 1).Text = "序号";
            grid.Cell(1, 2).Text = "时间";

            for (int i = 1; i <= Config.镀铜槽数; i++)
            {
                grid.Cell(1, 3 + (i - 1) * 4).Text = "镀铜" + i.ToString();
                grid.Cell(2, 3 + (i - 1) * 4).Text = "A1";
                grid.Cell(2, 4 + (i - 1) * 4).Text = "B1";
                grid.Cell(2, 5 + (i - 1) * 4).Text = "A2";
                grid.Cell(2, 6 + (i - 1) * 4).Text = "B2";
            }

            //合并单元格
            grid.Range(1, 1, 2, 1).Merge();
            grid.Range(1, 2, 2, 2).Merge();

            for (int i = 1; i <= Config.镀铜槽数; i++)
            {
                grid.Range(1, 3 + (i - 1) * 4, 1, 6 + (i - 1) * 4).Merge();
            }

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        private void UpdateGrid(FlexCell.Grid grid, List<CurrentRecordView> list)
        {
            list.Sort((a, b) => b.Time.CompareTo(a.Time));
            grid.AutoRedraw = false;

            grid.Rows = list.Count + 3;
            grid.Range(0, 0, grid.Rows - 1, grid.Cols - 1).Alignment = FlexCell.AlignmentEnum.CenterCenter;
            grid.FrozenRows = 2;

            for (int i = 0; i < list.Count; i++)
            {
                var record = list[i];

                int row = i + 3;
                int col = 1;

                grid.Cell(row, col++).Text = (i + 1).ToString();
                grid.Cell(row, col++).Text = record.Time.ToString("yyyy-MM-dd HH:mm:ss");

                for (int slot = 1; slot <= Config.镀铜槽数; slot++)
                {
                    var SlotCurrent = record.SlotCurrent[slot - 1];
                    grid.Cell(row, col++).Text = SlotCurrent.A[0].ToString();
                    grid.Cell(row, col++).Text = SlotCurrent.B[0].ToString();
                    grid.Cell(row, col++).Text = SlotCurrent.A[1].ToString();
                    grid.Cell(row, col++).Text = SlotCurrent.B[1].ToString();
                }
            }

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        //检测窗体是否打开
        private bool CheckForm(string formName)
        {
            bool bl = false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formName)
                {
                    bl = true;
                    f.Activate();
                    break;
                }
            }
            return bl;
        }

        //电流曲线
        private void tsCurve_Click(object sender, EventArgs e)
        {
            bool bl = CheckForm(typeof(FormCurrentCurve).Name);
            if (!bl)
            {
                FormCurrentCurve form = new FormCurrentCurve();
                form.ShowDialog();
            }
        }

        private void tsm今天_Click(object sender, EventArgs e)
        {
            var list = CurrentRecordView.Select(DateTime.Now);
            UpdateGrid(grid1, list);
        }

        private void tsm这两天_Click(object sender, EventArgs e)
        {
            var list = CurrentRecordView.Select(DateTime.Now, -2);
            UpdateGrid(grid1, list);
        }

        private void tsm这三天_Click(object sender, EventArgs e)
        {
            var list = CurrentRecordView.Select(DateTime.Now, -3);
            UpdateGrid(grid1, list);
        }

        private void tsRefresh_ButtonClick(object sender, EventArgs e)
        {

        }

        private void DTP_Start_ValueChanged(object sender, EventArgs e)
        {
            if (Math.Abs((DTP_Start.Value.Date - DTP_End.Value.Date.AddDays(1)).Days) > 7)
            {
                DTP_End.Value = DTP_Start.Value.Date.Date;
            }

            var list = CurrentRecordView.Select(DTP_Start.Value.Date, DTP_End.Value.Date.AddDays(1));
            UpdateGrid(grid1, list);
        }

        private void DTP_End_ValueChanged(object sender, EventArgs e)
        {
            if (Math.Abs((DTP_Start.Value.Date - DTP_End.Value.Date.AddDays(1)).Days) > 7)
            {
                MessageBox.Show("查询时间不能超过7天");
                return;
            }

            var list = CurrentRecordView.Select(DTP_Start.Value.Date, DTP_End.Value.Date.AddDays(1));
            UpdateGrid(grid1, list);
        }

        private void tsExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsExport_Click(object sender, EventArgs e)
        {
            if (grid1.ExportToExcel("", true, true))
            {
                MessageBox.Show("输出成功", "导出Excel", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
